package jwt.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;

public class Analyzer {

    public static List<String> splitText(String path){
        List<String> list = new ArrayList<>();
//        "D://list/文案.txt"
        File file = new File(path);
        try {
            StringBuilder sBuiler = new StringBuilder();
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()){
                sBuiler.append(scanner.nextLine());
            }
            String string = sBuiler.toString();
            if(string != null && !"".equals(string)){
                String[] split = string.replace("？","。").split("。");
                for (String s : split) {
                    list.add(s);
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (Exception e){
            throw new RuntimeException(e);
        }
        return list;
    }

    public static Map<String,String> analyzer(String path)  {
        Map<String,String> resMap = new HashMap<>();
//        "D://list/answer.txt"
        File file = new File(path);
        try {
            List<String> list = new ArrayList<>();
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()){
                list.add(scanner.nextLine());
            }

            for (int i = 0; i <list.size(); i++) {
                //对2取余，余数等于0的是问题，不等于0的是答案（简单测试）
                if(i%2 == 0){
                    resMap.put(list.get(i), list.get(i+1));
                }
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return resMap;
    }

    public static List<String> ikContent(String content){
        List<String> list = new ArrayList<>();
        System.out.println("原文件内容："+content);
        StringReader reader = new StringReader(content);
        try {
            IKSegmenter ik = new IKSegmenter(reader, true);
            Lexeme lexeme ;
            while ((lexeme = ik.next()) != null){
                list.add(lexeme.getLexemeText());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

}
